﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import flash.net.*;

    public class AMFConnection {

        private const TRANSPORT_FAILURE_AMF_FAULT:int = 1;
        private const TRANSPORT_FAILURE_NET_STATUS:int = 2;
        private const TRANSPORT_FAILURE_IO_ERROR:int = 4;
        private const TRANSPORT_FAILURE_ASYNC_ERROR:int = 4;
        private const TRANSPORT_FAILURE_SECURITY_ERROR:int = 5;

        protected var m_netConnection:NetConnection;
        protected var m_responder:Responder;
        protected var m_statusCallback:Function = null;
        protected var m_resultCallback:Function = null;
        protected var m_functionName:String;

        public function AMFConnection(_arg1:Function, _arg2:Function):void{
            this.m_netConnection = new NetConnection();
            super();
            this.m_statusCallback = _arg2;
            this.m_resultCallback = _arg1;
            this.m_responder = new Responder(this.onResult, this.onFault);
            this.m_netConnection.connect(Config.SERVICES_GATEWAY_PATH);
            this.m_netConnection.addEventListener(NetStatusEvent.NET_STATUS, this.onNetStatus, false, 0, true);
            this.m_netConnection.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError, false, 0, true);
            this.m_netConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this.onAsyncError, false, 0, true);
            this.m_netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError, false, 0, true);
        }
        protected function onResult(_arg1:Object):void{
            this.logEvent("Result received.");
            if (this.m_resultCallback != null){
                this.m_resultCallback(_arg1);
            };
        }
        protected function onFault(_arg1:Object):void{
            var _local2:String;
            this.logEvent("Fault received.");
            for (_local2 in _arg1) {
                this.logEvent(((_local2 + ": ") + _arg1[_local2]));
            };
            if (this.m_statusCallback != null){
                this.m_statusCallback(_arg1, this.TRANSPORT_FAILURE_AMF_FAULT, "transport failure");
            };
        }
        protected function onAsyncError(_arg1:IOErrorEvent):void{
            this.logEvent(("Async error: " + _arg1.toString()));
            if (this.m_statusCallback != null){
                this.m_statusCallback(_arg1, this.TRANSPORT_FAILURE_ASYNC_ERROR, "async failure");
            };
        }
        protected function onIOError(_arg1:IOErrorEvent):void{
            this.logEvent(("IO error: " + _arg1.toString()));
            if (this.m_statusCallback != null){
                this.m_statusCallback(_arg1, this.TRANSPORT_FAILURE_IO_ERROR, "io failure");
            };
        }
        protected function onNetStatus(_arg1:NetStatusEvent):void{
            this.logEvent(("Net status error: " + _arg1.toString()));
            if (this.m_statusCallback != null){
                this.m_statusCallback(_arg1, this.TRANSPORT_FAILURE_NET_STATUS, "net status failure");
            };
        }
        protected function onSecurityError(_arg1:SecurityErrorEvent):void{
            this.logEvent(("Security error: " + _arg1.toString()));
            if (this.m_statusCallback != null){
                this.m_statusCallback(_arg1, this.TRANSPORT_FAILURE_SECURITY_ERROR, "security failure");
            };
        }
        protected function logEvent(_arg1:String):void{
            GlobalEngine.log("AMFConnection", ((this.m_functionName + ": ") + _arg1));
        }
        public function call(_arg1:String, ... _args):void{
            var _local3:Array = [_arg1, this.m_responder];
            this.m_functionName = _arg1;
            _local3 = _local3.concat(_args);
            this.m_netConnection.call.apply(this.m_netConnection, _local3);
            this.logEvent(("Call: " + _arg1));
        }

    }
}//package Engine.Classes 
